﻿namespace Skymate.Commands
{
    using System.Collections.Generic;

    using Abp.Events.Bus;

    using Skymate.Lyjt.Core;

    using SuperSocket.Common;

    /// <summary>
    /// 信号方案查询应答
    /// </summary>
    public class TimingPlanQueryResponseCommand : BaseLightCommand
    {
        public TimingPlanQueryResponseCommand()
            : base(ProtocolConst.OptionType.QueryResponse, ProtocolConst.ObjectType.SignalTimingScheme)
        {
        }

        public override void ExecuteCommand(LightAppSession session, SemaphoreRequestInfo requestInfo)
        {
            var message = "信号方案查询应答";
            this.ShowLog(session.RemoteEndPoint.ToString(), message);

            var buf = requestInfo.Body;
            var length = buf[0];

            IList<TimingPlan> timeSpans = new List<TimingPlan>();
            for (var i = 0; i < length; i++)
            {
                var buffer = buf.CloneRange(1 + (TimingPlan.BufferSize * i), TimingPlan.BufferSize);
                timeSpans.Add(TimingPlan.Create(buffer));
            }

            EventBus.Default.Trigger(new TimingPlanQueryResponseEvent(
                session.RemoteEndPoint.ToString(), 
                timeSpans));
        }
    }
}